<?php
include_once "BaseM.php";
 
class CatM extends BaseM{
	
	private $table = "cat";
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function insert($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return mysql_insert_id( );
	}
	
	public function update($obj) {
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;
	}
	
	public function delete($key) {
		$sql="delete from $this->table where id = $key";
		$count = @mysql_query( $sql ) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $count;
	}
	
	public function searchBlood($blood, $searchStart, $searchEnd, $county, $liveZone){
		
		$whereStr = " 1 = 1 ";
		$whereStr .= ($blood <> 0 ? " and blood = $blood " : "");
		$whereStr .= $county != "" ? " and member_id in (select id from member where $county = ( select count from zipcode_tw where zip_code in ( $county ) ) ) " : "" ;
		$whereStr .= $liveZone != 0 ? " and member_id in (select id from member where live_zone in ( $liveZone )) " : "" ;
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql="
			select id as catId, name, (select county from member where id = cat.member_id) as county, photo,
				year( from_days( datediff( now( ), birthday ))) as old,
				case blood when 1 then 'A'
						   when 2 then 'B'
						   when 3 then 'AB'
				end blood
			from $this->table
			where $whereStr 
			limit $searchStart, $searchEnd ";
		$result = $this->querySQL($resultSql);
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function getList($memberId, $searchStart, $searchEnd){
		
		$whereStr = " member_id = $memberId ";
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql = " 
			select id as catId, name, (select county from member where id = cat.member_id )as county, photo,
				year( from_days( datediff( now( ), birthday ))) as old,
				case blood  when 1 then 'A'
							when 2 then 'B'
							when 3 then 'AB'
				end blood
			from $this->table
			where $whereStr
			limit $searchStart, $searchEnd ";
		$result = $this->querySQL($resultSql);
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function getBloodDetail($id){
		$sql="
			select 
				p.id as id, p.name as name, p.weight as weight, p.photo as photo,
				year( from_days( datediff( now( ), p.birthday ))) as old,
				case p.blood when 1 then 'A' when 2 then 'B' when 3 then 'AB' end blood,
				m.county as county, 
				m.realname as ownerRealName,
				m.nickname as ownerNickName,
				case m.gender when 1 then '男'
							when 2 then '女'
				end gender,
				m.county as ownerCounty,
				m.account as account,
				m.phone as phone,
				case m.show_line when 1 then m.no_line when 0 then '' end no_line,
				case m.show_fb when 1 then m.no_fb when 0 then '' end no_fb,
				case m.show_wechat when 1 then m.no_wechat when 0 then '' end no_wechat,
				case m.show_skype when 1 then m.no_skype when 0 then '' end no_skype,
				case m.show_gplus when 1 then m.no_gplus when 0 then '' end no_gplus
			from $this->table as p inner join member as m
			where p.id = $id and p.member_id = m.id";
		return $this->querySQL($sql, true);
	}
	
	public function getMyPetDetail($memberId, $id){
		$sql="
			select
				p.id as id, p.name as name, p.weight as weight, p.photo as photo,
				year( from_days( datediff( now( ), p.birthday ))) as old,
				case p.blood when 1 then 'A' when 2 then 'B' when 3 then 'AB' end blood,
				m.county as county
			from $this->table as p inner join member as m
			where p.id = $id and p.member_id = m.id and p.member_id = $memberId";
		return $this->querySQL($sql, true);
	}
	
}

?>